<?php

/**
 * This is the model Classify for table "{{Classify}}".
 *
 * The followings are the available columns in table '{{Classify}}':
 * @property integer $id
 * @property string $title
 * @property string $des
 * @property string $keyword
 * @property string $url
 */
Class Classify extends CActiveRecord
{
    /**
	 * Returns the static model of the specified AR Classify.
	 * @return Classify the static model Classify
	 */
	public static function model($ClassifyName=__Class__)
	{
		return parent::model($ClassifyName);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{Classify}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
            array('title, url', 'required'),
			array('title, keyword', 'length', 'max'=>80),
			array('des, url', 'length', 'max'=>128),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, title, des, keyword, url', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// Classify name for the relations automatically generated below.
		return array(
		'articles' => array(self::HAS_MANY, 'Article', 'class_id', 'order'=>'articles.create_time DESC','condition'=>'status='.Article::STATUS_PUBLISHED),
		'articleCount' => array(self::STAT, 'Article', 'class_id', 'condition'=>'status='.Article::STATUS_PUBLISHED),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => 'Title',
			'des' => 'Des',
			'keyword' => 'Keyword',
			'url' => 'Url',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('des',$this->des,true);
		$criteria->compare('keyword',$this->keyword,true);
		$criteria->compare('url',$this->url,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
     * 添加一个新文章到数组,在分类下发表文章的时候需要此方法。
     *给分类显示下添加文章的表单添加class_id，否则不能进行分类下添加文章。
     * @return $article boolen值。
     */
	public function addArticle($article)
	{
		$article->class_id=$this->id;
		return $article->save();
	}
        
    /**
	 * @返回字符串类型的 URL。它用来显示article的详情(对于含get的方法，可直接使用$model->url进行访问)
	 */
	public function getUrl3()
	{
		return Yii::app()->createUrl('Classify/view', array(
			'id'=>$this->id,
            'title'=>$this->title,
			'des'=>$this->des,
            'keyword'=>$this->keyword,
		));
	}
	
	/**
	 * 获取分类所有值
	 */
	public function getAllClass()
	{
	   $criteria=new CDbCriteria;
	   $criteria->order='id desc';
	   //$results=self::model()->findAll($criteria);	   
	   $results=CHtml::listData((self::model()->findAll($criteria)),'id','title');
	   return $results;
	}
	
	/**
	 * 此方法为匹配上相对应的分类名称
	 */
	public static function getMate($value)
	{
	    $result=self::model()->findByPk($value);   
		return  $result;
	}
}